package org.product

import grails.converters.*

class ProductController {
	static pagination = [ max: 10, offset: 0, sort: 'id', order: 'desc' ]
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	def filterPaneService
	
    def index = {
       redirect(action: "list", params: params)
    }
	
	def list = {
		params.max = params?.max?.toInteger() ?: pagination.max
		params.offset = params?.offset?.toInteger() ?: pagination.offset
        params.sort = params?.sort ?: pagination.sort
        params.order = params?.order ?: pagination.order

		if (params.applyFilter || params.partial) {
			render template:"list",model:[productInstanceList: Product.list(params), productInstanceTotal: Product.count()]
		}else{
		render view:"list",model:[productInstanceList: Product.list(params), productInstanceTotal: Product.count()]
		}
	}

    def create = {
        def productInstance = new Product()
        productInstance.properties = params
        return [productInstance: productInstance]
    }

    def save = {
        def productInstance = new Product(params)
        if (productInstance.save(flush: true)) {
            flash.message = message(code: 'default.created.message', args: [message(code: 'product.label', default: 'Product'), productInstance.id])
            redirect(action: "show", id: productInstance.id)
        }
        else {
            render(view: "create", model: [productInstance: productInstance])
        }
    }

    def show = {
        def productInstance = Product.get(params.id)
        if (!productInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'product.label', default: 'Product'), params.id])
            redirect(action: "list")
        }
        else {
            [productInstance: productInstance]
        }
    }
	def view = {
		def productInstance = Product.get(params.id)
		if (!productInstance) {
			render "query product error"
		}
		else {
			[productInstance: productInstance]
		}
	}
    def edit = {
        def productInstance = Product.get(params.id)
        if (!productInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'product.label', default: 'Product'), params.id])
            redirect(action: "list")
        }
        else {
            return [productInstance: productInstance]
        }
    }

    def update = {
        def productInstance = Product.get(params.id)
        if (productInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (productInstance.version > version) {
                    
                    productInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'product.label', default: 'Product')] as Object[], "Another user has updated this Product while you were editing")
                    render(view: "edit", model: [productInstance: productInstance])
                    return
                }
            }
            productInstance.properties = params
            if (!productInstance.hasErrors() && productInstance.save(flush: true)) {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'product.label', default: 'Product'), productInstance.id])
                redirect(action: "show", id: productInstance.id)
            }
            else {
                render(view: "edit", model: [productInstance: productInstance])
            }
        }
        else {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'product.label', default: 'Product'), params.id])
            redirect(action: "list")
        }
    }

    def delete = {
        def productInstance = Product.get(params.id)
        if (productInstance) {
            try {
                productInstance.delete(flush: true)
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'product.label', default: 'Product'), params.id])
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'product.label', default: 'Product'), params.id])
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'product.label', default: 'Product'), params.id])
            redirect(action: "list")
        }
    }
	
	def listByType = {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		params.offset = params?.offset?.toInteger() ?: pagination.offset
		params.sort = params?.sort ?: pagination.sort
		params.order = params?.order ?: pagination.order
		
		def productInstanceList =  Product.createCriteria().list(max:params.max,offset:params.offset,sort:params.sort,order:params.order){
			productType{
				eq("id", params?.productTypeId?.toLong())
				
			}
		}
		render template:"listByType",model:[productInstanceList: productInstanceList, productInstanceTotal: Product.count()]
	}
	
	def filter = {
		params.max = params?.max?.toInteger() ?: pagination.max
		params.offset = params?.offset?.toInteger() ?: pagination.offset
		params.sort = params?.sort ?: pagination.sort
		params.order = params?.order ?: pagination.order
		render( view:'list', 
	        model:[ productInstanceList: filterPaneService.filter( params, Product ), 
	        productInstanceTotal: filterPaneService.count( params, Product ), 
	        filterParams: org.common.FilterPaneUtils.extractFilterParams(params), 
	        params:params ] )
	}
	def search = {
		params.max = Math.min(params.max ? params.int('max'): 10, 100)
		params.offset = params?.offset ?.toInteger()?: pagination.offset
		def query = params.q
		if (!query) {
			return [:]
		}
		try {
			def searchResult = Product.search(query, params)
			render template:"search",model:[searchResult: searchResult]
		} catch (e) {
			e.printStackTrace();
			return [searchError: true]
		}
	}
}
